<?php
/**
 * 城池
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordfight{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	function index(){
		return $this->citygold();
	}
	/**
	 * 占领城池
	 * Enter description here ...
	 */
	function capture(){
		$to_key = isset($_POST['id']) ? trim($_POST['id']) : ''; //对方的uin
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$type_id = isset($_POST['type_id']) ? trim($_POST['type_id']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$task = isset($_POST['task']) ? trim($_POST['task']) : '';
		
		if (empty($to_key) || empty($type) || empty($city)){
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		//自己的key
		$key = 'user_'.$uin;
		//被占领城池的key
		$to_city_key = 'city_' .$to_key;
		//被占领uin的key
		$to_uin_key = 'user_' .$to_key;
		
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//我方君主信息
		$uin_result = App_Overlord_Query::oneUser($uin);
		
		//对方方君主信息
		$to_uin_result = App_Overlord_Query::oneUser($to_key);
		//对方城池信息
		$result = App_Overlord_Query::oneCity($to_key);

		if(empty($result)){
			return View::error('没有该玩家ID');
		}
		//收复时看
		if ($type == 'recover'){
			if (empty($result['Value']['hkee']['to_uin'])){
				return View::error('城池已发生变化!<br />', 0 ,array(
						array('name' => '<br />返回主城' , 'url' => '/app.php?m=overlordcity&amp;sid=' . sessionInfo('skey')),
						));
			}
		}
		if ($type != 'recover'){
			if(!empty($result['Value']['hkee']['to_uin']) && $result['Value']['hkee']['to_uin'] == -1){
				return View::error('此玩家正在任务中.');
			}
			//相同就返回主页
			if (!empty($result['Value']['hkee']['to_uin']) && $result['Value']['hkee']['to_uin'] == $uin) {
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser');
				break;
			}
		}
		//收复、解救时不限制
		if ($type != 'recover' && $type != 'defense' && $type != 'rescue'){
			//看是否有休战牌
			$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
			if (!empty($to_uin_result['Value']['cease']) && $to_uin_result['Value']['cease'] - $time > 0){
				return View::error('该玩家已使用休战令牌,你无法攻击.');
			}
			$lv = $uin_result['Value']['lv'] + 10;
			$to_lv = $to_uin_result['Value']['lv'] + 10;
			
			if ($uin_result['Value']['lv'] < 4){
				return View::error('你当前等级为'.$uin_result['Value']['lv'].'级,需升到4级才能攻占城池,你可以做任务升级<br />', 0 ,array(
						array('name' => '<br />去做任务' , 'url' => '/app.php?m=overlordtask&amp;a=opentask&amp;sid=' . sessionInfo('skey')),
						));
			}
			if ($to_uin_result['Value']['lv'] < 4){
				return View::error('对方等级低于4级.');
			}
			if ($to_uin_result['Value']['lv'] >= $lv){
				return View::error('你不能攻击高于你10级的城池.');
			}
			if ($uin_result['Value']['lv'] >= $to_lv){
				return View::error('你不能攻击低于你10级的城池.');
			}
			$count = 0;
			//查询个人武将
			$generals_result = App_Overlord_Query::oneUin_Generals($uin);
			if (!empty($generals_result['Value'])){
				foreach ($generals_result['Value'] as $k => $row){
					if (!empty($row['to_uin']) && $row['to_uin'] != $uin){
						$count += 1;
					}
				}	
			}
			//城池可攻占城池
			$city_count = App_Overlord_GameStart::getCityCount($uin_result['Value']['lv']);
			if ($count >= $city_count){
				return View::error('你已经占领了'.$count.'个城池,你当前等级不能拥有更多的城池了,赶快去升级吧！<br />', 0 ,array(
						array('name' => '<br />去做任务' , 'url' => '/app.php?m=overlordtask&amp;a=opentask&amp;sid=' . sessionInfo('skey')),
						));
			}
			
		}
		if ($type == 'rescue'){
			//print_r($result['Value'][$city]['to_uin']);
			//=================================判断是否需要解救==========================
			if (empty($result['Value'][$city]['to_uin']) || $result['Value'][$city]['to_uin'] == $to_key || $result['Value'][$city]['to_uin'] == $key){
				return View::error('无需再解救,返回刷新看看.');
			}
		}
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=dispatch&task='.$task.'&to_uin='.$to_key.'&type='.$type.'&city='.$city.'&type_id='.$type_id);
	}
	/**
	 * 战斗结果
	 * Enter description here ...
	 */
	function attr(){
		$attr = isset($_GET['key']) ? trim($_GET['key']) : '';
		$type = isset($_GET['type']) ? trim($_GET['type']) : 'show';
		$alter_provoke = isset($_GET['alter_provoke']) ? trim($_GET['alter_provoke']) : '';
		if (empty($attr)){
			return View::error('参数错误.');
		}
		
		$uin = SessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$battle = App_Overlord_Query::oneCollocate('app_overlord_battle' , $attr);
		$provoke = App_Overlord_GameStart::provoke();
		
		if (empty($battle)){
			return View::error('没有数据.');
		}
		//武将血量
		$hp = $battle['Value']['is_generals']['hp'];
		$is_hp = $battle['Value']['generals']['hp'];
		//计算武将伤害		
		foreach ($battle['Value']['is_studio_ap'] as $k => $row){
			$is_hp -= $row;
			$generals[$k]['is_studio_ap'] = $row;
			$generals[$k]['is_skill_studio_ap'] = $battle['Value']['is_skill_studio_ap'][$k];
			$generals[$k]['hp'] = $is_hp;
			if ($generals[$k]['hp'] < 0){
				$generals[$k]['hp'] = 0;
			}
			if (!empty($battle['Value']['skill_studio_defense'][$k])){
				$generals[$k]['skill_studio_defense'] = $battle['Value']['skill_studio_defense'][$k];
			}else{
				$generals[$k]['skill_studio_defense'] = 0;
			}
			if (!empty($battle['Value']['is_skill_studio_defense'][$k])){
				$generals[$k]['is_skill_studio_defense'] = $battle['Value']['is_skill_studio_defense'][$k];
			}else{
				$generals[$k]['is_skill_studio_defense'] = 0;
			}
			if (!empty($battle['Value']['studio_ap'][$k])){
				$generals[$k]['studio_ap'] = $battle['Value']['studio_ap'][$k];
				$hp -= $battle['Value']['studio_ap'][$k];
				$generals[$k]['is_hp'] = $hp;
			}else{
				$generals[$k]['studio_ap'] = 0;
				$generals[$k]['is_hp'] = 0;
			}
			if (!empty($battle['Value']['skill_studio_ap'][$k])){
				$generals[$k]['skill_studio_ap'] = $battle['Value']['skill_studio_ap'][$k];
			}
		}
		if (empty($battle['Value']['barracks'][2]['a'])){
			$battle['Value']['barracks'][2]['a'] = array();
		}
		if (empty($battle['Value']['barracks'][2]['b'])){
			$battle['Value']['barracks'][2]['b'] = array();
		}
		$list_a = array_merge(array($battle['Value']['barracks'][0]),$battle['Value']['barracks'][2]['a']);		
		$list_b = array_merge(array($battle['Value']['barracks'][1]),$battle['Value']['barracks'][2]['b']);
		if ($battle['Value']['hp'] > $battle['Value']['is_hp']){
			$a = $list_a;
			$b = $list_b;
			$list_a = $b;
			$list_b = $a;
			$battle_a = $battle['Value']['barracks'][2]['b'];
			$battle_b = $battle['Value']['barracks'][2]['a'];
			//特殊技能打兵过程
			if (empty($battle['Value']['barracks'][2]['b_special'])){
				$a_special = array();
			}else{
				$a_special = $battle['Value']['barracks'][2]['b_special'];
			}
			if (empty($battle['Value']['barracks'][2]['a_special'])){
				$b_special = array();
			}else{
				$b_special = $battle['Value']['barracks'][2]['a_special'];
			}
			//武将技能打兵过程
			if (empty($battle['Value']['barracks'][2]['b_barracks'])){
				$a_barracks = array();
			}else{
				$a_barracks = $battle['Value']['barracks'][2]['b_barracks'];
			}
			if (empty($battle['Value']['barracks'][2]['a_barracks'])){
				$b_barracks = array();
			}else{
				$b_barracks = $battle['Value']['barracks'][2]['a_barracks'];
			}
			$battle['Value']['barracks']['a_count'] = array_sum($battle['Value']['barracks'][1]);
			$battle['Value']['barracks']['b_count'] = array_sum($battle['Value']['barracks'][0]);
		}else{
			
			$battle['Value']['barracks']['a_count'] = array_sum($battle['Value']['barracks'][0]);
			$battle['Value']['barracks']['b_count'] = array_sum($battle['Value']['barracks'][1]);
				
			//取战斗过程
			$battle_a = $battle['Value']['barracks'][2]['a'];
			$battle_b = $battle['Value']['barracks'][2]['b'];
	
			//特殊技能打兵过程
			if (empty($battle['Value']['barracks'][2]['a_special'])){
				$a_special = array();
			}else{
				$a_special = $battle['Value']['barracks'][2]['a_special'];
			}
			if (empty($battle['Value']['barracks'][2]['b_special'])){
				$b_special = array();
			}else{
				$b_special = $battle['Value']['barracks'][2]['b_special'];
			}
			//武将技能打兵过程
			if (empty($battle['Value']['barracks'][2]['a_barracks'])){
				$a_barracks = array();
			}else{
				$a_barracks = $battle['Value']['barracks'][2]['a_barracks'];
			}
			if (empty($battle['Value']['barracks'][2]['b_barracks'])){
				$b_barracks = array();
			}else{
				$b_barracks = $battle['Value']['barracks'][2]['b_barracks'];
			}
		}	
		//兵数 相减
		foreach ($list_a as $k => $row){
			if (!empty($list_a[$k+1]) || !empty($list_b[$k+1])){
				if (empty($list_a[$k+1])){
					$list_a[$k+1] = $list_a[$k];
				}
				if (empty($list_b[$k+1])){
					$list_b[$k+1] = $list_b[$k];
				}
				if (!empty($a_barracks[$k])){
					$row[$a_barracks[$k][0]] -= $a_barracks[$k][1];
				}
				if (!empty($b_barracks[$k])){
					$list_b[$k][$b_barracks[$k][0]] -= $b_barracks[$k][1];
				}
				foreach ($row as $j => $rs){
					if (!empty($a_special[$k])){
						$rs -= $a_special[$k][0][$j];
					}
					if (!empty($b_special[$k])){
						$list_b[$k][$j] -= $b_special[$k][0][$j];
					}
					$ap_a[$k][$j] = $rs - $list_a[$k+1][$j];
					$ap_b[$k][$j] = $list_b[$k][$j] - $list_b[$k+1][$j];
				}
			}
		}
		foreach ($ap_a as $k => $row){
			if (empty($battle_a[$k])){
				if (empty($battle_a[$k - 1])){
					$battle_a[$k] = array(0 , 0 , 0 , 0);
				}else{
					$battle_a[$k] = $battle_a[$k - 1];
				}
			}
			if (empty($battle_b[$k])){
				if (empty($battle_b[$k - 1])){
					$battle_b[$k] = array(0 , 0 , 0 , 0);
				}else{
					$battle_b[$k] = $battle_b[$k - 1];
				}
			}
			if (empty($a_special[$k])){
				$a_special[$k] = 0;
			}
			if (empty($b_special[$k])){
				$b_special[$k] = 0;
			}
			if (empty($a_barracks[$k])){
				$a_barracks[$k] = 0;
			}
			if (empty($b_barracks[$k])){
				$b_barracks[$k] = 0;
			}
			$pk[$k]['a'] = $battle_a[$k];
			$pk[$k]['sum_a'] = array_sum($battle_a[$k]);
			$pk[$k]['ap_a'] = $row;
			$pk[$k]['a_special'] = $a_special[$k];
			$pk[$k]['a_barracks'] = $a_barracks[$k];
			$pk[$k]['b'] = $battle_b[$k];
			$pk[$k]['sum_b'] = array_sum($battle_b[$k]);
			$pk[$k]['ap_b'] = $ap_b[$k];
			$pk[$k]['b_special'] = $b_special[$k];
			$pk[$k]['b_barracks'] = $b_barracks[$k];
		}
		/*
		$count = count($pk);
		//兵类倒序显示
		foreach (array_reverse($pk) as $k => $row){
			$list[$count - $k] = $row;
		}*/
		$smarty = loadSmarty();
		$generals_count = count($generals);
		$fail = array();
		//查询损失兵种数量
		if ($battle['Value']['uin'] == $uin){
			$sum_array = array_reverse($list_a);
			$suma = array_sum($sum_array[0]);	
			$smarty->assign('suma',$suma);
			
			if ($battle['Value']['state'] == 1 && !empty($battle['Value']['b_name']['uin'])){
				$city_provoke = app_Overlord_Query::oneCity($battle['Value']['b_name']['uin']);
				if (!empty($city_provoke)){
					$str = $city_provoke['Value']['hkee']['provoke'];
					$content = $str;
					if( substr($str , 0 , 3) == '[[#' && substr($str , 4 , 3) == '#]]') {
					    // 替换一下就好
					    $id = substr($str , 3 , 1);
					    if (!empty($provoke[$id])) $content = $provoke[$id];
					}
					
					$smarty->assign('city_provoke',$content);
				}
			}
		}else{
			$sum_array = array_reverse($list_b);
			$sumb = array_sum($sum_array[0]);	
			$smarty->assign('sumb',$sumb);
		}
		/*
		//武将倒序显示
		foreach (array_reverse($generals) as $k => $row){
			$generals_list[$generals_count - $k] = $row;
		}*/	

		//根据战略
		$a_tactic = App_Overlord_GameStart::getStrategy($battle['Value']['generals']['strategy']);
		$b_tactic = App_Overlord_GameStart::getStrategy($battle['Value']['is_generals']['strategy']);
		$a_tactic_sum = intval($battle['Value']['barracks']['a_count'] * (1 - $a_tactic / 100));
		$b_tactic_sum = intval($battle['Value']['barracks']['b_count'] * (1 - $b_tactic / 100));
		$smarty->assign('a_tactic_sum',$a_tactic_sum);
		$smarty->assign('b_tactic_sum',$b_tactic_sum);
		$smarty->assign('list',$battle['Value']);
		$smarty->assign('info',$battle['Value']['a_name']);
		$smarty->assign('result',$pk);
		$smarty->assign('hp',$battle['Value']['hp']);
		$smarty->assign('is_hp',$battle['Value']['is_hp']);
		$smarty->assign('generals',$generals);
		$smarty->assign('is_info', $battle['Value']['c_name']);
		$smarty->assign('type' , $type);
		$smarty->assign('id' , $attr);
		$smarty->assign('uin' , $uin);
		$smarty->assign('provoke' , $provoke);
		return $smarty->fetch('app/Overlord/fight_result.tpl.php');
	}
	/**
	 * 重写fight
	 * 重要：三个变量,A去攻打B,B这时候被C占领（C有可能等于B）
	 */
	function fight() {
		$infantry = isset($_POST['infantry']) ? intval($_POST['infantry']) : null;
		$cavalryman = isset($_POST['cavalryman']) ? intval($_POST['cavalryman']) : null;
		$toxotae = isset($_POST['toxotae']) ? intval($_POST['toxotae']) : null;
		$special = isset($_POST['special']) ? intval($_POST['special']) : null;
		$to_uin = isset($_POST['id']) ? trim($_POST['id']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$type_id = isset($_POST['type_id']) ? trim($_POST['type_id']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$generals_id = isset($_POST['generals']) ? trim($_POST['generals']) : '';// $generals 表示自己武将列表的索引

		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
	
		// 把 $to_uin 转成数字,最好前台传过来的就转好
		//$to_uin = str_replace('user_' , '' , $to_uin);
		//$to_uin = intval($to_uin);
		

		// ===============================  判断传过来的post参数是否正确 =====================================
		if (empty($to_uin) || empty($type) || empty($city) || empty($generals_id)) {
			return View::error('参数错误');
		}

		// 三个变量,A去攻打B,B这时候被C占领（C有可能等于B）
		$a_uin = SessionInfo('uin');
		$b_uin = $to_uin;

		// 判断是否自己打自己了
		if ($type != 'recover' && $b_uin == $a_uin) {
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser');
			break;
		}

		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		
		// ===============================  判断出征的武将是否合法 =====================================
		// 查询个人武将表
		$a_generals = App_Overlord_Query::oneUin_Generals($a_uin);
		if (empty($a_generals['Value']) || empty($a_generals['Value'][$generals_id])){
			return View::error('你还没有武将');
		}

		if ($a_generals['Value'][$generals_id]['state'] != 0){
			return View::error('该名武将无法出征');
		}
		$a_generals['Value'][$generals_id]['id'] = $generals_id;
		$a_general_info = $a_generals['Value'][$generals_id];	// a 的出征武将信息
		if ($a_general_info['physical'] < 4){
			return View::error('你的武将体力不足4点,无法出征');
		}
		
		// ===============================  判断出征的兵力是否合法 =====================================
		// 获取自己的信息
		$a_user_info = App_Overlord_Query::oneUser($a_uin);
		if (empty($a_user_info['Value'])){
			return View::error('获取君主信息失败.');
		}
		
		// 查询兵种类型
		$bingzhong_list = App_Overlord_GameStart::getBarracksType($a_user_info['Value']['country']);

		$max_count = App_Overlord_GameStart::getGeneralsBarracksLv($a_general_info['lv']);
		//获取最少带兵数
		$min_generals_count = $max_count['count'] * 0.1;
		
		// 查询武将资料  这个我看你取了后面没用到,好像就是为了取武将姓名。 如果是的话,我建议你把姓名也存到你自己的武将列表里去
		// $generals = App_Overlord_GameStart::getOneOverlord('app_overlord_generals' , $a_general_info['generals']);

		// 计算总兵数量
		$bing_count = $infantry + $cavalryman + $toxotae + $special;
		if ($bing_count < $min_generals_count){
			return View::error('你的武将至少带领'.$min_generals_count.'个士兵');
		}
		//数量超过最大
		if ($bing_count > $a_general_info['make_count']){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=fight_brr&to_uin='.$to_uin.'&type='.$type.'&type_id='.$type_id.'&city=' .$city .'&generals=' .$generals_id .'&task=' .$task);
			break;
		}
		// 查询个人兵营
		$a_barracks_info = App_Overlord_Query::oneSoldiers($a_uin);
		$a_barracks_now_array = array($infantry , $cavalryman , $toxotae , $special);	// 记录这次打仗的兵力
		$a_barracks_new_array = array();	// 记录兵营剩余的士兵数量
		$header = 0;
		foreach ($bingzhong_list as $k => $row){
			if (!empty($row[$a_barracks_info['Value'][$k]['type']])) {
				$a_barracks_info['Value'][$k]['barracks'] = $row;

				// 判断选用的兵大于自身拥有的兵
				if ($a_barracks_info['Value'][$k]['count'] < $$a_barracks_info['Value'][$k]['type']){
					$header = 1;
				}

				$a_barracks_new_array[$k]['type'] =  $a_barracks_info['Value'][$k]['type'];
				$a_barracks_new_array[$k]['count'] =  $a_barracks_info['Value'][$k]['count'] - $$a_barracks_info['Value'][$k]['type'];
			}
		}
		if (!empty($header)){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=fight_brr&to_uin='.$to_uin.'&type='.$type.'&type_id='.$type_id.'&city=' .$city .'&generals=' .$generals_id .'&task=' .$task);
			break;
		}
		// ===============================  检查对方君主的数据  =====================================
		$b_user_info = App_Overlord_Query::oneUser($b_uin);
		
		// ===============================  检查对方城池的数据  =====================================
		$b_city_array = App_Overlord_Query::oneCity($b_uin);
		
		
		if(empty($b_user_info)){
			return View::error('没有该玩家ID');
		}
		if(empty($b_city_array['Value'][$city])) {
			return View::error('该玩家城池不存在');
		}
		// 判断是否自己打自己了
		if ($b_city_array['Value'][$city]['to_uin'] == $a_uin) {
			return View::error('你已占领该城池!', 0 ,array(
						array('name' => '<br />返回主城' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
						));
		}
		$b_city_info = $b_city_array['Value'][$city];
		
		// 获取 c_uin
		$c_uin = $b_city_info['to_uin'];
		if ($type == 'rescue'){
			if ( empty($c_uin) || $c_uin == $b_uin ) {
				return View::error('无需再解救,返回刷新看看');
			}
		}
		if (!empty($c_uin)){
		// ===============================  检查C方君主的数据  =====================================
			$c_user_info = App_Overlord_Query::oneUser($c_uin);
			if(empty($c_user_info)){
				return View::error('没有该玩家ID');
			}
		}
		// ===============================  检查限制  =====================================
		// 限制级别 (收复、解救的时候不做限制)
		if ($type != 'recover' && $type != 'rescue'){
			$lv = $a_user_info['Value']['lv'] + 10;
			$to_lv = $b_user_info['Value']['lv'] + 10;
			if ($a_user_info['Value']['lv'] < 4){
				return View::error('君主等级不低于4级才能发生战斗。');
			}
			if ($b_user_info['Value']['lv'] < 4){
				return View::error('对方等级低于4级。');
			}
			if ($b_user_info['Value']['lv'] >= $lv){
				return View::error('你不能攻击高于你10级的城池。');
			}
			if ($a_user_info['Value']['lv'] >= $to_lv){
				return View::error('你不能攻击低于你10级的城池。');
			}
			$count = 0;
			if (!empty($a_generals['Value'])){
				foreach ($a_generals['Value'] as $k => $row){
					if (!empty($row['to_uin']) && $row['to_uin'] != $a_uin){
						$count += 1;
					}
				}	
			}
			//城池可攻占城池
			$city_count = App_Overlord_GameStart::getCityCount($a_user_info['Value']['lv']);
			if ($count >= $city_count){
				return View::error('你已经占领了'.$count.'个城池,你当前等级不能拥有更多的城池了,赶快去升级吧！<br />', 0 ,array(
						array('name' => '<br />去做任务' , 'url' => '/app.php?m=overlordtask&amp;a=opentask&amp;sid=' . sessionInfo('skey')),
						));
			}
			//看对方是否有免战牌
			if (!empty($b_user_info['Value']['cease']) && $b_user_info['Value']['cease'] - $time > 0){
				return View::error('该玩家已使用休战令牌,你无法攻击。');
			}
		}

		// 查询可出征次数
		$a_fight_info = App_Overlord_Query::oneCollocate('app_overlord_fight_count' , 'fight_' . $a_uin);
		if ($a_fight_info['Value']['count'] <= 0){
			return View::error('你今天已经不能再出征了(使用讨伐令可以增加出征次数)', 0 ,array(
				array('name' => '使用出征令' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;type=prop&amp;sid=' . sessionInfo('skey')),
					));
		}
		$a_fight_info['Value']['count'] -= 1;	// 记录剩余战斗次数

		//获取任务类型
		loadLib('App_Overlord_Task');
		$task_type = App_Overlord_Task::getTaskType();
		//完成攻占城池任务
		if ($type == 'capture'){
			App_Overlord_Task::getGo('main' , $task_type['main']['attack']);
		}
		// ===============================  战斗开始  =====================================
		// 判断是否是空城
		if(empty($c_uin)) {
			// 直接占领
			$a_general_info['make'] = $a_barracks_now_array;  //修改武将带兵数
			$a_general_info['state'] = 1;					  //修改武将状态
			$a_general_info['to_uin'] = $b_uin;			  //修改武将占领城池的ID
			$a_general_info['time'] = $time + 2 * 3600;       //修改武将占领时间
			$a_general_info['city_time'] = $time;       //修改武将占领城池需调兵时间
			$a_generals['Value'][$generals_id] = $a_general_info;  //给武将重新赋值
			
			$b_city_info['to_uin'] = $a_uin;				  //修改对方城池to_uin
			$b_city_info['provoke'] = '[[#-1#]]';                  //默认挑衅语   我了个擦!你也太菜了!
			$b_city_info['generals'] = $generals_id;		  //修改对方城池的武将ID为我的索引ID
			$b_city_info['generals_name'] = $a_general_info['name'];//修改对方城池的武将名称
			$b_city_array['Value'][$city] = $b_city_info;     //给占领的城池重新赋值
		} else {
			$c_generals_info = array();	// 记录C的武将信息
			$c_barracks_now_array = array();	// 记录驻防的兵力
			
			loadLib('App_Overlord_Collocate');
			//获取技能列表
			$data = App_Overlord_Collocate::getSkill();
			
			// 如果有驻防武将
			if (!empty($b_city_info['generals'])){
				$c_generals = App_Overlord_Query::oneUin_generals($c_uin);
			
				foreach ($c_generals['Value'] as $k => $row) {
					$cur_general_to_uin = $row['to_uin'];	// 该武将驻防的uin
					if ($b_uin == $cur_general_to_uin && $b_city_info['generals'] == $k){
						$c_generals_info = $row;
						$c_generals_index = $k;	// 记录武将的索引
						$c_generals_info['id'] = $c_generals_index;
						break;
					}
				}	
		
				// 记录驻防的兵力
				if (!empty($c_generals_info)){
					foreach ($c_generals_info['make'] as $k => $row){
						$c_barracks_now_array[] = $row;
					}
				}

				//武将先打
				$general_pk = App_Overlord_GameStart::getAttackGeneral($a_general_info['skills'] , round($a_general_info['ap']), round($a_general_info['hp']), round($a_general_info['defense']), round($c_generals_info['ap']), round($c_generals_info['hp']), round($c_generals_info['defense']), $c_generals_info['skills']);

			// =================================获取武将技能 ==========================	
			//A方武将
				if (!empty($a_general_info['skills']['strengthen'])){
					foreach ($a_general_info['skills']['strengthen'] as $k => $row){
						//将领战斗失败士兵加属性技能
						if ($k == 'break'){
							$a_skill['break']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['break']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['break'] =  0;
						}
						//将领战斗胜利士兵加属性技能
						if ($k == 'defeat'){
							$a_skill['defeat']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['defeat']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['defeat'] =  0;
						}
						//将领战斗胜利士兵加属性技能
						if ($k == 'defeat'){
							$a_skill['defeat']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['defeat']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['defeat'] =  0;
						}
						//战斗目的,攻占
						if ($type == 'capture'){
							//攻占城池士兵加攻技能
							if ($k == 'carry'){
								$a_skill['carry']['name'] =  $data['strengthen'][$k][$row]['name'];
								$a_skill['carry']['num'] =  $data['strengthen'][$k][$row]['num'];
							}else{
								$a_skill['carry'] =  0;
							}
							//攻占城池士兵加防技能
							if ($k == 'consolidate'){
								$a_skill['consolidate']['name'] =  $data['strengthen'][$k][$row]['name'];
								$a_skill['consolidate']['num'] =  $data['strengthen'][$k][$row]['num'];
							}else{
								$a_skill['consolidate'] =  0;
							}
						}else{
							$a_skill['carry'] =  0;
							$a_skill['consolidate'] =  0;
						}
						//弓兵攻击专精
						if ($k == 'toxotae_ap'){
							$a_skill['toxotae_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['toxotae_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['toxotae_ap'] =  0;
						}
						//弓兵防御专精
						if ($k == 'toxotae_defense'){
							$a_skill['toxotae_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['toxotae_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['toxotae_defense'] =  0;
						}
						//步兵攻击专精
						if ($k == 'infantry_ap'){
							$a_skill['infantry_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['infantry_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['infantry_ap'] =  0;
						}
						//步兵防御专精
						if ($k == 'infantry_defense'){
							$a_skill['infantry_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['infantry_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['infantry_defense'] =  0;
						}
						//骑兵攻击专精
						if ($k == 'cavalryman_ap'){
							$a_skill['cavalryman_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['cavalryman_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['cavalryman_ap'] =  0;
						}
						//骑兵防御专精
						if ($k == 'cavalryman_defense'){
							$a_skill['cavalryman_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['cavalryman_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['cavalryman_defense'] =  0;
						}
						//特种兵攻击专精
						if ($k == 'special_ap'){
							$a_skill['special_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['special_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['special_ap'] =  0;
						}
						//特种兵防御专精
						if ($k == 'special_defense'){
							$a_skill['special_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$a_skill['special_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$a_skill['special_defense'] =  0;
						}
					}
				}else{
					$a_skill = 0; 
				}
				
				//武将打兵技能
				if (!empty($a_general_info['skills']['study'])){
					//a没中闭月斩 ,技能可用,武将hp不小于零
					if (empty(App_Overlord_GameStart::$a_special) && $general_pk['hp'] >= 0){
						foreach ($a_general_info['skills']['study'] as $k => $row){
							//判断是否有武将附带伤害
							if ($k == 'barracks' || $k == 'barracks_1' || $k == 'barracks_2'){
								$a_skill_barracks[$k]['name'] =  $data['study'][$k][$row]['name'];
								$a_skill_barracks[$k]['odds'] =  $data['study'][$k][$row]['odds'];
								$a_skill_barracks[$k]['num'] =  $data['study'][$k][$row]['num'];
							}
						}
						if (empty($a_skill_barracks)){
							$a_skill_barracks = 0;
						}
					}
				}else{
					$a_skill_barracks = 0; 
				}
			//武将打兵技能,
				if (!empty($a_general_info['skills']['special'])){
					
					//C没中闭月斩 ,技能可用,武将hp不小于零
					if (empty(App_Overlord_GameStart::$a_special) && $general_pk['hp'] >= 0){
						foreach ($a_general_info['skills']['special'] as $k => $row){
							//判断是否有武将附带伤害,飞刀扇击
							if ($k == 'strike'){
								$a_skill_special[$k]['name'] =  $data['special'][$k][$row]['name'];
								$a_skill_special[$k]['odds'] =  $data['special'][$k][$row]['odds'];
								$a_skill_special[$k]['num'] =  $data['special'][$k][$row]['num'];
							}
						}
					}
					if (empty($a_skill_special)){
						$a_skill_special = 0;
					}
				}else{
					$a_skill_special = 0; 
				}
		
				
			//C方武将
				if (!empty($c_generals_info['skills']['strengthen'])){
					foreach ($c_generals_info['skills']['strengthen'] as $k => $row){
						//将领战斗失败士兵加属性技能
						if ($k == 'break'){
							$c_skill['break']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['break']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['break'] =  0;
						}
						//将领战斗胜利士兵加属性技能
						if ($k == 'defeat'){
							$c_skill['defeat']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['defeat']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['defeat'] =  0;
						}
						//将领战斗胜利士兵加属性技能
						if ($k == 'defeat'){
							$c_skill['defeat']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['defeat']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['defeat'] =  0;
						}
						//驻防城池士兵加攻技能
						if ($k == 'against'){
							$c_skill['against']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['against']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['against'] =  0;
						}
						//驻防城池士兵加防技能
						if ($k == 'defend'){
							$c_skill['defend']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['defend']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['defend'] =  0;
						}
						//弓兵攻击专精
						if ($k == 'toxotae_ap'){
							$c_skill['toxotae_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['toxotae_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['toxotae_ap'] =  0;
						}
						//弓兵防御专精
						if ($k == 'toxotae_defense'){
							$c_skill['toxotae_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['toxotae_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['toxotae_defense'] =  0;
						}
						//步兵攻击专精
						if ($k == 'infantry_ap'){
							$c_skill['infantry_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['infantry_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['infantry_ap'] =  0;
						}
						//步兵防御专精
						if ($k == 'infantry_defense'){
							$c_skill['infantry_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['infantry_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['infantry_defense'] =  0;
						}
						//骑兵攻击专精
						if ($k == 'cavalryman_ap'){
							$c_skill['cavalryman_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['cavalryman_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['cavalryman_ap'] =  0;
						}
						//骑兵防御专精
						if ($k == 'cavalryman_defense'){
							$c_skill['cavalryman_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['cavalryman_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['cavalryman_defense'] =  0;
						}
						//特种兵攻击专精
						if ($k == 'special_ap'){
							$c_skill['special_ap']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['special_ap']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['special_ap'] =  0;
						}
						//特种兵防御专精
						if ($k == 'special_defense'){
							$c_skill['special_defense']['name'] =  $data['strengthen'][$k][$row]['name'];
							$c_skill['special_defense']['num'] =  $data['strengthen'][$k][$row]['num'];
						}else{
							$c_skill['special_defense'] =  0;
						}
					}
				}else{
					$c_skill = 0; 
				}
				//武将打兵技能
				if (!empty($c_generals_info['skills']['study'])){
					
					//C没中闭月斩 ,技能可用,武将hp不小于零
					if (empty(App_Overlord_GameStart::$c_special) && $general_pk['is_hp'] >= 0){
						foreach ($c_generals_info['skills']['study'] as $k => $row){
							//判断是否有武将附带伤害
							if ($k == 'barracks' || $k == 'barracks_1' || $k == 'barracks_2'){
								$c_skill_barracks[$k]['name'] =  $data['study'][$k][$row]['name'];
								$c_skill_barracks[$k]['odds'] =  $data['study'][$k][$row]['odds'];
								$c_skill_barracks[$k]['num'] =  $data['study'][$k][$row]['num'];
							}
						}
					}
					if (empty($c_skill_barracks)){
						$c_skill_barracks = 0;
					}
				}else{
					$c_skill_barracks = 0; 
				}
				//武将打兵技能,
				if (!empty($c_generals_info['skills']['special'])){
					
					//C没中闭月斩 ,技能可用,武将hp不小于零
					if (empty(App_Overlord_GameStart::$c_special) && $general_pk['is_hp'] >= 0){
						foreach ($c_generals_info['skills']['special'] as $k => $row){
							//判断是否有武将附带伤害,飞刀扇击
							if ($k == 'strike'){
								$c_skill_special[$k]['name'] =  $data['special'][$k][$row]['name'];
								$c_skill_special[$k]['odds'] =  $data['special'][$k][$row]['odds'];
								$c_skill_special[$k]['num'] =  $data['special'][$k][$row]['num'];
							}
						}
					}
					if (empty($c_skill_special)){
						$c_skill_special = 0;
					}
				}else{
					$c_skill_special = 0; 
				}
		
				//我方武将赢,兵先出手
				if ($general_pk['is_hp'] <= $general_pk['hp']){
					$barracks_pk = App_Overlord_GameStart::getAttackbarracks(1 , $a_skill , $c_skill , $a_barracks_now_array, $c_barracks_now_array , $a_skill_barracks , $c_skill_barracks , $a_skill_special , $c_skill_special , $a_general_info['strategy'] , $c_generals_info['strategy']);
					//计算打斗剩余的兵力
					$a_sum_barracks = array_sum($barracks_pk[0]); //A的兵力,统计剩余兵力总数
					$b_sum_barracks = array_sum($barracks_pk[1]); //C的兵力,统计剩余兵力总数
					$a_count_barracks = $barracks_pk[0];  //A的兵力
					$b_count_barracks = $barracks_pk[1];  //C的兵力
					//战斗记录
					$barracks_pk[1] = $a_barracks_now_array;
					$barracks_pk[0] = $c_barracks_now_array;		
					$general_pk['barracks'] = $barracks_pk;/**/	
				}else{
					$barracks_pk = App_Overlord_GameStart::getAttackbarracks(2 , $c_skill , $a_skill , $c_barracks_now_array, $a_barracks_now_array , $c_skill_barracks , $a_skill_barracks , $c_skill_special , $a_skill_special , $c_generals_info['strategy'] , $a_general_info['strategy']);
					//计算打斗剩余的兵力
					$a_sum_barracks = array_sum($barracks_pk[1]); //A的兵力,统计剩余兵力总数
					$b_sum_barracks = array_sum($barracks_pk[0]); //C的兵力,统计剩余兵力总数
					$a_count_barracks = $barracks_pk[1];  //A的兵力
					$b_count_barracks = $barracks_pk[0];  //C的兵力
					//战斗记录
					$barracks_pk[1] = $c_barracks_now_array;
					$barracks_pk[0] = $a_barracks_now_array;
					//判断输赢
					if ($barracks_pk[3] == 1){
						$barracks_pk[3] = 2 ;
					}else{
						$barracks_pk[3] = 1 ;
					}
					$general_pk['barracks'] = $barracks_pk;/**/
				}

		// ==================================战斗结束计算死亡兵数获得经验===============================		
				//A的死亡总兵数
				$a_sum = array_sum($a_barracks_now_array) - $a_sum_barracks;
				//C的死亡总兵数
				$c_sum = array_sum($c_barracks_now_array) - $b_sum_barracks;	
				//取武将战斗获得经验对应表
				loadLib('App_Overlord_Collocate');
				
				//记录信息
				$introduce = array();
				
		// ===================================取得武将可升最高等级===============================
				//取得A武将可升最高级别
				$a_generals_lv = App_Overlord_GameStart::getGeneralsLevel($a_user_info['Value']['lv']);
				
				if (!empty($c_user_info)){
					$temp_lv = $c_user_info['Value']['lv'];
				}else{
					$temp_lv = $b_user_info['Value']['lv'];
				}
				//取得C武将可升最高级别
				$c_generals_lv = App_Overlord_GameStart::getGeneralsLevel($temp_lv);
				
		// ==================================战斗结束判断战斗目的===============================		
				//A赢
				if ($barracks_pk[3] == 1){
					//取得A武将对应数据
					$a_base = App_Overlord_Collocate::getGeneralsFightExp($a_general_info['lv']);
					
					//取得A君主对应经验
					$a_user_base = App_Overlord_Collocate::getUserFightExp($a_user_info['Value']['lv']);
					
					
					//取得A君主对应功勋
					$a_exploits_base = App_Overlord_Collocate::getExploitsFightExp($a_user_info['Value']['lv']);
					
					//取得A君主经验
					$a_user_exp = $a_user_base[$b_user_info['Value']['lv']];
					//取得A君主功勋
					$a_user_exploits = $a_exploits_base[$b_user_info['Value']['lv']];
					//A的武将经验
					$a_exp = intval($c_sum/$a_base['victory']);
					//限制A获得最高经验
					if ($a_exp > $a_base['a_max_exp']){
						$a_exp = $a_base['a_max_exp'];
					}
					
					//C的武将经验
					$c_exp = intval($c_sum/$a_base['defeated']);
					//限制C获得最高经验
					if ($c_exp > $a_base['b_max_exp']){
						$c_exp = $a_base['b_max_exp'];
					}
					
					//A武将已是最高级别,无经验
					if ($a_generals_lv <= $a_general_info['lv']){
						$a_exp = 0;
					}	
					//A的武将
					$a_general_info['exp'] += $a_exp;		//武将得到的经验
								
					//解救
					if ($type == 'rescue'){
						//武将获得经验
						$a_generals['Value'][$generals_id] = $a_general_info;
						//城池
						$b_city_info['to_uin'] = '';	 //置空城池
						$b_city_info['generals'] = '';		  //置空城池的武将ID
						$b_city_info['generals_name'] = '';//置空城池的武将名称
						$b_city_array['Value'][$city] = $b_city_info;     //给城池重新赋值
						foreach ($a_count_barracks as $k => $row){
							$a_barracks_new_array[$k]['count'] += $row;  //把武将兵力还给兵营
						}
				
					//占领或收复
					}elseif ($type == 'capture' || $type == 'recover'){
						$a_general_info['make'] = $a_count_barracks;  //修改武将带兵数
						$a_general_info['state'] = 1;					  //修改武将状态
						$a_general_info['to_uin'] = $b_uin; //修改武将占领城池的ID
						$a_general_info['time'] = $time + 2 * 3600;       //修改武将占领时间
						$a_general_info['city_time'] = $time;       //修改武将占领城池需调兵时间
						$a_generals['Value'][$generals_id] = $a_general_info;  //给武将重新赋值
						
						//城池
						$b_city_info['to_uin'] = $a_uin;	 //修改A城池to_uin
						$b_city_info['provoke'] = '[[#-1#]]';         //默认挑衅语   我了个擦!你也太菜了!
						$b_city_info['generals'] = $generals_id;		  //修改A城池的武将ID为我的索引ID
						$b_city_info['generals_name'] = $a_general_info['name'];//修改对方城池的武将名称
						$b_city_array['Value'][$city] = $b_city_info;     //给占领的城池重新赋值
					
					// ==================================80%获得物品 ===================================
						$odds = rand(0 , 99);
						//产生几率
						if($odds <= 79) {
							if ($odds <= 19){
								$draw = array('material' => array(0 , 1)); //20%几率获得金玉石
							}elseif ($odds <= 29){
								$draw = array('treasure' => array(0 , 1)); //10%几率获得铁宝箱
							}elseif ($odds <= 39){
								$draw = array('treasure' => array(1 , 1)); //10%几率获得铜宝箱
							}elseif ($odds <= 49){
								$draw = array('treasure' => array(2 , 1)); //10%几率获得银宝箱
							}elseif ($odds <= 57){
								$draw = array('treasure' => array(3 , 1)); //8%几率获得金宝箱
							}elseif ($odds <= 65){
								$draw = array('treasure' => array(4 , 1)); //8%几率获得铂金宝箱
							}elseif ($odds <= 70){
								$draw = array('key' => array(0 , 1)); //5%几率获得铁钥匙
							}elseif ($odds <= 74){
								$draw = array('key' => array(0 , 1)); //4%几率获得铜钥匙
							}elseif ($odds <= 76){
								$draw = array('key' => array(0 , 1)); //2%几率获得银钥匙
							}elseif ($odds <= 78){
								$draw = array('key' => array(0 , 1)); //2%几率获得金钥匙
							}elseif ($odds <= 79){
								$draw = array('key' => array(0 , 1)); //1%几率获得铂金钥匙
							}		    
						}
						
						if (!empty($draw)){
							//查询背包数据
							$items_info = App_Overlord_Query::oneItems($a_uin);
							if (empty($items_info)){
								$items_info['Value'] = array();
								$code = App_Overlord_Query::addItems('app_overlord_items', 'items_' . $a_uin, $items_info['Value']);
							}	
							$data = App_Overlord_Collocate::getGoods();
							foreach ($draw as $k => $row){
								$data_inof['name'] = $data[$k][$row[0]]['name'];
								$data_inof['count'] = $row[1];
								if (!empty($items_info['Value'][$k][$row[0]])){
									$items_info['Value'][$k][$row[0]]['count'] += $row[1];
								}else{
									$items_info['Value'][$k][$row[0]]['count'] = $row[1];
								}
							}
							//获得物品记录
							$general_pk['data'] = $data_inof;
							//修改背包
							$code = App_Overlord_Query::alterItems('app_overlord_items', 'items_' . $a_uin, $items_info['Value']);
						}
					}else{
						return View::error('数据异常');
					}
					
					//C武将已是最高级别,无经验
					if ($c_generals_lv <= $c_generals['Value'][$c_generals_index]['lv']){
						$c_exp = 0;
					}
					//C的武将
					$c_generals['Value'][$c_generals_index]['exp'] += $c_exp;  //C的武将经验
					$c_generals['Value'][$c_generals_index]['make'] = array();          //C武将兵力置空
					$c_generals['Value'][$c_generals_index]['state'] = 0;			//C武将改为空闲
					$c_generals['Value'][$c_generals_index]['to_uin'] = '';			//C武将城池状态置空
					$general_pk['state'] = 1;  //战斗记录 状态：赢
			
					$c_sum = array_sum($c_barracks_now_array) - $b_sum_barracks; //记录C损失兵数
					
					if ($b_uin == $a_uin){
						$introduce['name'] = '孤注一掷,君上兵少食尽痛失附属城池,我军损兵'.$c_sum.'个.';
					}elseif ($b_uin == $c_uin){
						$introduce['name'] = '背水一战,光复自己的城池,我军损兵'.$c_sum.'个.';
					}else{
						$introduce['name'] = '横扫雷霆之势,君上兵薄将弱痛失主城,我军损兵'.$c_sum.'个.';
					}
				//	$introduce['name'] = '你的武将'.$c_generals_info['name'].'驻守的主城,被' . $a_user_infos['name'] .'攻占了';
					if (!empty($b_count_barracks)){
						// 查询c的兵营
						$c_barracks_info = App_Overlord_Query::oneSoldiers($c_uin);
						foreach ($b_count_barracks as $k => $row){
							$c_barracks_info['Value'][$k]['count'] += $row;  //把武将兵力还给兵营
						}
						//修改C的兵营
						$code = App_Overlord_Query::alterSoldiers($c_uin, $c_barracks_info['Value']);
					}
				}elseif ($barracks_pk[3] == 2){
				//C赢
					//取得C武将对应数据
					$c_base = App_Overlord_Collocate::getGeneralsFightExp($c_generals_info['lv']);
					//A的武将经验
					$a_exp = intval($a_sum/$c_base['defeated']);
					//限制A获得最高经验
					if ($a_exp > $c_base['b_max_exp']){
						$a_exp = $c_base['b_max_exp'];
					}
					//A君主经验为0
					$a_user_exp = 0;
					//A君主功勋为0
					$a_user_exploits = 0;
					//C的武将经验
					$c_exp = intval($a_sum/$c_base['victory']);
					//限制C获得最高经验
					if ($c_exp > $c_base['a_max_exp']){
						$c_exp = $c_base['a_max_exp'];
					}
					//C武将已是最高级别,无经验
					if ($c_generals_lv <= $c_generals_info['lv']){
						$c_exp = 0;
					}
					//A武将已是最高级别,无经验
					if ($a_generals_lv <= $a_generals['Value'][$generals_id]['lv']){
						$a_exp = 0;
					}
					$a_generals['Value'][$generals_id]['exp'] += $a_exp; //A武将获得的经验
					//C的武将
					$c_generals_info['make'] = $b_count_barracks;          //C武将兵力
					$c_generals_info['exp'] += $c_exp;
					$c_generals['Value'][$c_generals_index] = $c_generals_info; //C武将重新赋值
				
					$general_pk['state'] = 2;  //战斗记录 状态：输
					if (!empty($a_count_barracks)){
						foreach ($a_count_barracks as $k => $row){
							$a_barracks_new_array[$k]['count'] += $row;  //看A武将还有多少兵,还给兵营
						}
					}
				//	$introduce['name'] = '你的武将'.$c_generals_info['name'].'驻守的主城,抵御了' . $a_user_infos['name'] .'的攻击';
					
					$c_sum = array_sum($c_barracks_now_array) - $b_sum_barracks; //记录C损失兵数
					
					$introduce['uin'] = $b_uin;
		//			$introduce['name'] = $a_user_infos['name'] . '攻击了你,你胜利了,你损失了'.$c_sum.'个士兵.';
					
					if ($b_uin == $a_uin){
						$introduce['name'] = '敌军图谋收复自己的主城,君上深谋远虑,用兵如神,敌军溃不成军宣降,我军损兵'.$c_sum.'个.';
					}elseif ($b_uin == $c_uin){
						$introduce['name'] = '贼党密谋进攻君上的主城,君上权略善战,足智多谋,敌军弹尽粮绝而退,我军损兵'.$c_sum.'个.';
					}else{
						$introduce['name'] = '贼党伺机侵袭君上的附属城池,君上运筹帷幄,决胜千里,敌军不敌而遁,我军损兵'.$c_sum.'个.';
					}
				
				}
		
				//武将体力恢复时间
				$physical_time = $time + 3600;
				//A武将有经验加成技能
				if (!empty($a_general_info['skills']['study'])){
					foreach ($a_general_info['skills']['study'] as $k => $skills_row){
						if ($k == 'exp'){
							//经验加成
							$skills_num = $data['study'][$k][$skills_row]['num'];
							$a_exp += $a_exp * $skills_num/100;
						}elseif ($k == 'train_hp'){
							//减短体力恢复时间
							$skills_num = $data['study'][$k][$skills_row]['num'];
							$physical_time -= intval(3600 * $skills_num/100);
						}
					}
				}
				//C武将有经验加成技能
				if (!empty($c_generals_info['skills']['study'])){
					foreach ($c_generals_info['skills']['study'] as $k => $skills_row){
						if ($k == 'exp'){
							//经验加成
							$skills_num = $data['study'][$k][$skills_row]['num'];
							$c_exp += $c_exp * $skills_num/100;
						}
					}
				}

		
				$a_generals['Value'][$generals_id]['physical'] -= 2; 				//修改A武将体力值
				//满体力时重新赋值
				if ($a_generals['Value'][$generals_id]['physical'] == 8){
					$a_generals['Value'][$generals_id]['physical_time'] = $physical_time; //体力恢复时间
				}
				$general_pk['generals'] = $a_general_info;  //记录A武将
				$general_pk['is_generals'] = $c_generals_info; //记录C武将
				$general_pk['uin'] = $a_uin; //记录A的UIN
				$general_pk['a_exp'] = $a_exp; //记录A武将得到的经验
				$general_pk['a_user_exp'] = $a_user_exp; //记录A君主得到的经验
				$general_pk['a_exploits'] = $a_user_exploits; //记录A君主得到的功勋
				
				$general_pk['c_exp'] = $c_exp; //记录C武将得到的经验
				$general_pk['c_user_exp'] = 0; //记录A君主得到的经验
				$general_pk['c_exploits'] = 0; //记录A君主得到的功勋
			}
		}		
	
		// ==================================战斗结束修改数据===============================
		//修改A武将
		$code = App_Overlord_Query::alterUin_Generals($a_uin, $a_generals['Value']);
		//A剩余兵力
		$code = App_Overlord_Query::alterSoldiers($a_uin, $a_barracks_new_array);

		//C占领对象
		$code = App_Overlord_Query::alterCity($b_uin, $b_city_array['Value']);
		
		//修改出征次数
		$code = App_Overlord_Query::alterCollocate('app_overlord_fight_count', 'fight_' . $a_uin, $a_fight_info['Value']);
/**/		
		// 判断是否是空城
		if (empty($c_uin)){
			//空城直接跳转到提示页面,无战斗过程
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type='.$type.'&id='.$to_uin);
			break;
		}else{
			
			//A君主获得的经验
			$a_user_info['Value']['exp'] += $a_user_exp;
			
			//A君主获得的功勋
			$a_user_info['Value']['exploits'] += $a_user_exploits;
			
			//修改A君主信息
			$code = App_Overlord_Query::alterUser($a_uin, $a_user_info['Value']);
			
			//C武将修改剩余兵力
			$code = App_Overlord_Query::alterUin_Generals($c_uin, $c_generals['Value']);

			$introduce['time'] = $time;
			$introduce['battle'] = 'battle_' .$c_uin .'_' . $a_uin .'_'. $time;
			//战场UIN
			$introduce['to_uin'] = $b_uin;	
			//对手UIN
			$introduce['rivaluin'] = $a_uin;	
			//对手名称,
			$introduce['rivalname'] = $a_user_info['Value']['name'];	
			//写入情报
			App_Overlord_GameStart::getCharacters($c_uin, $introduce);
			$general_pk['a_name']['uin'] = $a_uin;
			$general_pk['a_name']['name'] = $a_user_info['Value']['name'];
			$general_pk['b_name']['uin'] = $b_uin;
			$general_pk['b_name']['name'] = $b_user_info['Value']['name'];
			$general_pk['c_name']['uin'] = $c_uin;
			$general_pk['c_name']['name'] = $c_user_info['Value']['name'];
			$general_pk['type'] = $type;
			//修改战斗记录
			$battle_info['Value'] = $general_pk;  //赋值战斗记录
			$code =App_Overlord_Query::addCollocate('app_overlord_battle', 'battle_' .$c_uin .'_' . $a_uin .'_'. $time, $battle_info['Value']);
			
			//跳转到战斗显示页面
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordfight&a=attr&key=battle_' .$c_uin .'_' . $a_uin .'_'. $time);
/**/		}
	}
	/**
	 * 撤防提示
	 */
	function garrison_pege(){
		$to_uin = isset($_POST['id']) ? trim($_POST['id']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$smarty = loadSmarty();
		$smarty->assign('id',$to_uin);
		$smarty->assign('city',$city);
		$smarty->assign('type',$type);
		return $smarty->fetch('app/Overlord/garrison_pege.tpl.php');
	}
	/**
	 * 城池撤防
	 * Enter description here ...
	 */
	function garrison(){
		$to_uin = isset($_POST['id']) ? trim($_POST['id']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		$uin = SessionInfo('uin');
		// ===============================  判断传过来的post参数是否正确 =====================================
		if (empty($to_uin) || empty($city) || empty($type)){
			return View::error('参数错误.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		// ===============================  检查对方城池的数据  =====================================
		$city_info = App_Overlord_Query::oneCity($to_uin);
		
		if (empty($city_info)){
			return View::error('该城池不存在.');
		}
		
		if (empty($city_info['Value'][$city]['to_uin']) || empty($city_info['Value'][$city]['generals']) || $city_info['Value'][$city]['to_uin'] != $uin){
			return View::error('你还未驻防该城池');
		}
	/**/	
		
		$city_to_uin = $city_info['Value'][$city]['to_uin']; //取得被退防的uin
		$generals_index = $city_info['Value'][$city]['generals']; //取得武将索引
		
		
		// ===============================  检查武将的数据  =====================================
		$generals_info = App_Overlord_Query::oneUin_Generals($city_to_uin);
		if (empty($generals_info)){
			return View::error('你还没有武将.');
		}
		if (empty($generals_info['Value'][$generals_index])){
			return View::error('数据异常.');
		}
		/*
		$generals_time = 0;
		if (!empty($generals_info['Value'][$generals_index]['city_time'])){
			$generals_time = $generals_info['Value'][$generals_index]['city_time'];
			//5分钟的调整时间
			$generals_time += 5 * 60;
		}
		if ($time < $generals_time){
			$end_time = App_Overlord_GameStart::getEndDate($generals_time);
			if (!empty($end_time)){
				if ($end_time[0] != '00'){
					$city_time = $end_time[0] + 1 .'小时';
				}elseif($end_time[1] != '00') {
					$city_time = $end_time[1] + 1 .'分钟';
				}elseif ($end_time[2] != '00'){
					$city_time = '1分钟';
				}
			}
			return View::error('军队正在调整中,'.$city_time.'后才能撤防.');
		}
		*/	
		$make = $generals_info['Value'][$generals_index]['make'];  //获取武将所带的兵
		// ===============================  检查兵营的数据  =====================================
		$barracks_info = App_Overlord_Query::oneSoldiers($city_to_uin);
	
		if (empty($barracks_info)){
			return View::error('你还没有兵营.');
		}
		if (empty($barracks_info['Value'])){
			return View::error('数据异常.');
		}
	
		foreach ($make as $k => $row){
			$barracks_info['Value'][$k]['count'] += $row;  //把武将兵力还给兵营
		}
		
		$city_info['Value'][$city]['generals'] = ''; //撤防城池武将置空
		$city_info['Value'][$city]['to_uin'] = '';	 //撤防城池置空
		$city_info['Value'][$city]['generals_name'] = ''; //撤防武将名称置空
		$generals_info['Value'][$generals_index]['make'] = array(); //带兵数置空
		$generals_info['Value'][$generals_index]['state'] = 0; //状态置空闲
		$generals_info['Value'][$generals_index]['to_uin'] = ''; //城池置空
		
		
		//城池修改
		App_Overlord_Query::alterCity($to_uin , $city_info['Value']);
		//武将修改
		App_Overlord_Query::alterUin_Generals($city_to_uin , $generals_info['Value']);
		//兵营修改
		App_Overlord_Query::alterSoldiers($city_to_uin , $barracks_info['Value']);
		
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type='.$type.'&id='.$to_uin);
/**/		
	}
	/**
	 * 调兵
	 */
	function deploy(){
		$infantry = isset($_POST['infantry']) ? intval($_POST['infantry']) : null;
		$cavalryman = isset($_POST['cavalryman']) ? intval($_POST['cavalryman']) : null;
		$toxotae = isset($_POST['toxotae']) ? intval($_POST['toxotae']) : null;
		$special = isset($_POST['special']) ? intval($_POST['special']) : null;
		$to_uin = isset($_POST['id']) ? trim($_POST['id']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$generals_id = isset($_POST['generals']) ? trim($_POST['generals']) : '';		// $generals 表示自己武将列表的索引
		$task = isset($_POST['task']) ? trim($_POST['task']) : '';
		$barracks_now_array = array($infantry , $cavalryman , $toxotae , $special);	// 记录这次调兵的兵力
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		$uin = SessionInfo('uin');
		// ===============================  判断传过来的post参数是否正确 =====================================
		if (empty($to_uin) || empty($type) || empty($city) || empty($generals_id)) {
			return View::error('参数错误');
		}
		
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		// ===============================  检查君主的数据  =====================================
		$user_info = App_Overlord_Query::oneUser($uin);
		
		// ===============================  检查对方城池的数据  =====================================
		$city_info = App_Overlord_Query::oneCity($to_uin);
		
		if (empty($city_info)){
			return View::error('该城池不存在.');
		}
		
		if ($city_info['Value'][$city]['to_uin'] != -1 && $type != 'defense'){
			if (empty($city_info['Value'][$city]['generals']) || $city_info['Value'][$city]['to_uin'] != $uin){
				return View::error('城池发生了变化,返回刷新试试.');
			}
			$city_to_uin = $city_info['Value'][$city]['to_uin']; //取得被退防的uin
			$generals_index = $city_info['Value'][$city]['generals']; //取得武将索引
		}else{
			$city_to_uin = $uin;
			$generals_index = $generals_id;
			if ($type != 'defense'){
				$type = 'recover';
			}
		}
		
		// ===============================  检查武将的数据  =====================================
		$generals_info = App_Overlord_Query::oneUin_Generals($city_to_uin);
		if (empty($generals_info)){
			return View::error('你还没有武将.');
		}
		
		if (empty($generals_info['Value'][$generals_index]) || $generals_index != $generals_id){
			return View::error('数据异常.');
		}
		/*
		$generals_time = 0;
		if (!empty($generals_info['Value'][$generals_index]['city_time'])){
			$generals_time = $generals_info['Value'][$generals_index]['city_time'];
			//5分钟的调整时间
			$generals_time += 5 * 60;
		}
		if ($time < $generals_time){
			$end_time = App_Overlord_GameStart::getEndDate($generals_time);
			if (!empty($end_time)){
				if ($end_time[0] != '00'){
					$city_time = $end_time[0] + 1 .'小时';
				}elseif($end_time[1] != '00') {
					$city_time = $end_time[1] + 1 .'分钟';
				}elseif ($end_time[2] != '00'){
					$city_time = '1分钟';
				}
			}
			return View::error('军队正在调整中,'.$city_time.'后才能调兵.');
		}
		*/
		// 查询武将可带兵数
		$bing_max_count = App_Overlord_GameStart::getGeneralsBarracksLv($generals_info['Value'][$generals_index]['lv']);
		//获取最少带兵数
		$bing_min_count = $bing_max_count['count'] * 0.1;
		// 计算总兵数量
		$bing_count = $infantry + $cavalryman + $toxotae + $special;
		if ($bing_count < $bing_min_count){
			return View::error('你的武将至少带领'. $bing_min_count .'个士兵');
		}
		if ($bing_count > $generals_info['Value'][$generals_index]['make_count']){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=fight_brr&to_uin='.$to_uin.'&type='.$type.'&type_id='.$type_id.'&city=' .$city .'&generals=' .$generals_id .'&task=' .$task);
			break;
		}
		// 查询兵种类型
		$bingzhong_list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
		
		$make = $generals_info['Value'][$generals_index]['make'];  //获取武将所带的兵
		// ===============================  检查兵营的数据  =====================================
		$barracks_info = App_Overlord_Query::oneSoldiers($city_to_uin);
		
		if (empty($barracks_info['Value'])){
			return View::error('数据异常.');
		}
		$header = 0;
		$barracks_new_array = array();	// 记录兵营剩余的士兵数量
		foreach ($bingzhong_list as $k => $row){
			
			if (!empty($row[$barracks_info['Value'][$k]['type']])) {
				$barracks_info['Value'][$k]['barracks'] = $row;

				// 判断选用的兵大于自身拥有的兵
				if ($barracks_info['Value'][$k]['count'] < $$barracks_info['Value'][$k]['type']){
					$header = 1;
				}

				$barracks_new_array[$k]['type'] =  $barracks_info['Value'][$k]['type'];
				if (!empty($city_info['Value'][$city]['to_uin']) && $city_info['Value'][$city]['to_uin'] != -1){
					if (empty($make[$k])){
						$make[$k] = 0;
					}	
					$barracks_new_array[$k]['count'] =  $barracks_info['Value'][$k]['count'] - $$barracks_info['Value'][$k]['type'] + $make[$k];
				
				}else{
					$barracks_new_array[$k]['count'] =  $barracks_info['Value'][$k]['count'] - $$barracks_info['Value'][$k]['type'];
				}
			}
		}
		if (!empty($header)){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=fight_brr&to_uin='.$to_uin.'&type='.$type.'&type_id='.$type_id.'&city=' .$city .'&generals=' .$generals_id .'&task=' .$task);
			break;
		}
		$generals_info['Value'][$generals_index]['make'] = $barracks_now_array;  //重新给武将赋值
		
		$barracks_info['Value'] = $barracks_new_array; //重新给兵营赋值
		
		if ($type == 'defense'){
			if ($city_info['Value'][$city]['to_uin'] == $uin){
				return View::error('你的将领'.$city_info['Value'][$city]['generals_name'].'已在驻防中!');
			}
			//驻防自己的城池
			$city_info['Value'][$city]['to_uin'] = $city_to_uin;
			$city_info['Value'][$city]['generals'] = $generals_index;
			$city_info['Value'][$city]['generals_name'] = $generals_info['Value'][$generals_index]['name'];
			
			//给武将赋值
			$generals_info['Value'][$generals_index]['to_uin'] = $city_to_uin;
			$generals_info['Value'][$generals_index]['state'] = 1;
			$generals_info['Value'][$generals_index]['time'] = $time;       //修改武将占领时间
			$generals_info['Value'][$generals_index]['city_time'] = 0;       //修改武将占领城池需调兵时间
			//城池修改
			$code = App_Overlord_Query::alterCity($city_to_uin, $city_info['Value']);	
		}
		
		//武将修改
		$code = App_Overlord_Query::alterUin_Generals($city_to_uin, $generals_info['Value']);
		
		//兵营修改
		$code = App_Overlord_Query::alterSoldiers($city_to_uin, $barracks_info['Value']);
		$code = App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type='.$type.'&id='.$to_uin);
	}
	/**
	 * 手动修改挑衅语
	 */
	function write(){
		$to_uin = isset($_POST['to_uin']) ? intval($_POST['to_uin']) : null;
		$attr = isset($_POST['id']) ? trim($_POST['id']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
	
		if (empty($to_uin)) {
			return View::error('参数错误!');
		}
		loadLib('App_Overlord_Query');
		loadLib('App_Overlord_GameStart');
		$uin = SessionInfo('uin');
		$city_info = App_Overlord_Query::oneCity($to_uin);
		$user_info = App_Overlord_Query::oneUser($to_uin);
		if (empty($city_info) || empty($user_info)){
			return View::error('数据异常!');
		}
		if (empty($city_info['Value']['hkee']['to_uin']) || $city_info['Value']['hkee']['to_uin'] != $uin) {
			return View::error('你未攻占Ta的城池!');
		}
		$smarty = loadSmarty();
		$smarty->assign('to_uin',$to_uin);
		$smarty->assign('user_info',$user_info);
		$smarty->assign('attr',$attr);
		$smarty->assign('type',$type);
		return $smarty->fetch('app/Overlord/write.tpl.php');
	}
	/**
	 * 修改挑衅语
	 * Enter description here ...
	 */
	function provoke(){
		$to_uin = isset($_POST['to_uin']) ? intval($_POST['to_uin']) : null;
		$attr = isset($_POST['id']) ? trim($_POST['id']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$provoke_id = isset($_POST['provoke']) ? intval($_POST['provoke']) : 0;
		$content = isset($_POST['content']) ? trim($_POST['content']) : '';
		
		if (empty($to_uin)) {
			return View::error('参数错误!');
		}
		loadLib('App_Overlord_Query');
		loadLib('App_Overlord_GameStart');
		$uin = SessionInfo('uin');
		$city_info = App_Overlord_Query::oneCity($to_uin);
		if (empty($city_info)){
			return View::error('数据异常!');
		}
		if (empty($city_info['Value']['hkee']['to_uin']) || $city_info['Value']['hkee']['to_uin'] != $uin) {
			return View::error('你未攻占Ta的城池!');
		}
		
		$provoke = App_Overlord_GameStart::provoke();
		if (empty($provoke[$provoke_id])) $provoke_id = 0;
		//修改挑衅语
		if (empty($content)){
			$city_info['Value']['hkee']['provoke'] = '[[#' . $provoke_id . '#]]';
		}else{
			if (strlen($content) >39){
				return View::error('最多能输入13个汉字');
			}
			$city_info['Value']['hkee']['provoke'] = $content;
		}
		$code = App_Overlord_Query::alterCity($to_uin , $city_info['Value']);
		if ($type == 'attr'){
			//跳转看记录
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordfight&a=attr&key='.$attr);
		}else{
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcity&a=to_city&to_uin='.$to_uin);
		}
	}
}
# end of script